// pages/others/others.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePaths: [],
    sortlist: ['SAAS', '独立', '腾讯云', '其他'],
    selectedIdx: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      wtype: options.wtype
    })
    wx.setNavigationBarTitle({
      title: options.wtype == 0 ? '需求工单' : options.wtype == 1 ? '投诉工单' : '其他工单',
    })
  },

  optionInput: function(e) {
    this.setData({
      option: e.detail.value
    })
  },

  textareaInput: function(e) {
    this.setData({
      textarea: e.detail.value
    })
  },

  accountInput: function(e) {
    this.setData({
      account: e.detail.value
    })
  },

  passwordInput: function(e) {
    this.setData({
      password: e.detail.value
    })
  },

  bindSelect: function(e) {
    console.log(e)
    this.setData({
      selectedIdx: e.detail.value
    })
  },

  //上传图片
  upload: function() {
    var that = this;
    var pics = this.data.tempFilePaths;
    wx.chooseImage({
      count: 6 - pics.length,
      success: function(res) {
        console.log(res);
        pics.push.apply(pics, res.tempFilePaths);
        that.setData({
          tempFilePaths: pics
        })
      },
    })
  },

  //浏览图片
  preview: function(e) {
    var index = e.currentTarget.dataset.index;
    var url = this.data.tempFilePaths
    wx.previewImage({
      urls: url,
      current: url[index]
    })
  },

  //删除图片
  deleteImg: function(e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var temp = this.data.tempFilePaths;
    temp.splice(index, 1);
    this.setData({
      tempFilePaths: temp
    })
  },

  // 创建工单
  createWorkList: function() {
    if (this.data.textarea == '' || this.data.textarea == null) {
      app.showConfirm('提示', '请输入工单描述', false);
    } else {
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
      var that = this;
      that.data.uploadImg = [];
      if (that.data.tempFilePaths != null && that.data.tempFilePaths != '' && that.data.tempFilePaths.length != 0) {
        for (var i = 0; i < that.data.tempFilePaths.length; i++) {
          if (that.data.tempFilePaths[i].indexOf('wxfile://') > -1 || that.data.tempFilePaths[i].indexOf('http://tmp') > -1) {
            app.server.uploadImage(that.data.tempFilePaths[i], {
              work_order_type: 2
            }).then(function(res) {
              that.data.uploadImg.push(res.data.url)
              console.log(res);
              if (that.data.uploadImg.length == that.data.tempFilePaths.length) {
                that.doCreate();
              }
            }, function(res) {
              app.showConfirm('提示', '图片上传失败，请重试', false);
              wx.hideLoading();
            });
          }
        }
      } else {
        that.doCreate();
      }
    }
  },

  // 提交工单
  doCreate: function() {
    var data = {
      point_url: app.server.agentApi.submitWorkOrder,
      openid: app.globalData.openid,
      type: parseInt(this.data.wtype) + 1,
      content: this.data.textarea,
      options: this.data.wtype == 0 ? this.data.sortlist[this.data.selectedIdx] : this.data.option,
      user_account: this.data.account,
      user_password: this.data.password,
      pic_list: JSON.stringify(this.data.uploadImg).toString()
    };
    app.server.getConnect(data).then(function(res) {
      console.log(res);
      app.showConfirm('提示', '工单提交成功', false, function() {
        wx.switchTab({
          url: '/pages/agent/workList/workList',
        })
      })
      wx.hideLoading();
    }, function(res) {
      console.log(res);
      wx.hideLoading();
      app.showConfirm('提示', res.data.msg, false);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    
  }
})
